package br.com.iswe.gwt.ui.client;

import br.com.iswe.gwt.ui.client.comp.ISWEButton;
import br.com.iswe.gwt.util.client.CommonConstants;
import br.com.iswe.gwt.util.client.Listener;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ButtonBase;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class PnBotoesInferior extends FlexTable {

	protected static CommonConstants commonConstants = (CommonConstants)GWT.create(CommonConstants.class);
	
	private static final String OK = "1";
	private static final String SEPARATOR = "sprt";
	
	protected int colButton = 1;
	protected ClickListener listener  = null;
	
	protected int separador = 1;
	
	public PnBotoesInferior(){
		//Cria as imagens apartir do bundle.
		setStyleName("pnBotoesInferior");
		Label lbEspaco = new Label("");
		lbEspaco.setWidth("100%");
		setWidget(0, 0, lbEspaco);
	}

	public void setFecharPainelListener(final ClickListener listenerPainelFiltro) {
		this.listener = new ClickListener(){
			public void onClick(Widget sender) {
				listenerPainelFiltro.onClick(sender);
			}
		};
		separador = 2;
		//Caso tenha listener utiliza a barra com a seta.
		Label lbEspaco = new Label("");
		lbEspaco.setWidth("100%");
		setWidget(0, 0, lbEspaco);
		getFlexCellFormatter().setWidth(0, 0, "90%");
	}

	public void add(Widget w) {
		super.add(w);
	}
	
	public ButtonBase getButton(int idx){
		return (Button)getWidget(0, idx);
	}
	
	public void addButton(ISWEButton button) {
		if(colButton > 1){
			final Image img = addSeparador();
			if(button instanceof ISWEWidget){
				String separador = (String)((ISWEWidget)button).getWidgetMetaInfo().get(SEPARATOR);
				if(separador != null && OK.equals(separador)){
					((ISWEWidget)button).addSetVisibleListener(new Listener(){public void execute(Object parametro) {
						Boolean bln = (Boolean)parametro;
						img.setVisible(bln.booleanValue());
					}});
				}
			}
		}
		setWidget(0, colButton++, button);
	}
	
	private Image addSeparador() {
		Image imgSeparador = null; 
		if(separador == 1){
			imgSeparador = ISWEImageFactory.getCommonImageBundle().separador01().createImage();
		}
		else if(separador == 2){
			imgSeparador = ISWEImageFactory.getCommonImageBundle().separador02().createImage();
		}
		
		setWidget(0, colButton++, imgSeparador);
		return imgSeparador;
	}

	///////////////////////////////////////
	/// Metodos para criar botoes ///
	///////////////////////////////////////
	public static ISWEButton createButtonSalvar() {
		ISWEButton button = new ISWEButton(commonConstants.salvar()){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}
	
	public static ISWEButton createButtonDeletar() {
		ISWEButton button = new ISWEButton(commonConstants.deletar()){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}
	
	public static ISWEButton createButtonPesquisar() {
		ISWEButton button = new ISWEButton(commonConstants.pesquisar()){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}
	
	public static ISWEButton createButtonLimpar() {
		ISWEButton button = new ISWEButton(commonConstants.limpar()){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}

	public static ISWEButton createButtonFechar() {
		ISWEButton button = new ISWEButton(commonConstants.fechar()){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}
	
	public static ISWEButton createButtonCancelar() {
		ISWEButton button = new ISWEButton(commonConstants.cancelar()){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}
	
	public static ISWEButton createButtonVerificar() {
		ISWEButton button = new ISWEButton("Verificar"){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}

	public static ISWEButton createButtonAdicionar() {
		ISWEButton button = new ISWEButton("Adicionar"){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}

	public static ISWEButton createButtonLogin() {
		ISWEButton button = new ISWEButton("Efetuar Login"){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}

	public static ISWEButton createButtonProximoPasso() {
		ISWEButton button = new ISWEButton("Proximo Passo"){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}

	public static ISWEButton createButton(String text) {
		ISWEButton button = new ISWEButton(text){
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				//logica de esconder o separador aqui
			}
		};
		return button;
	}
}
